package com.yzj.android.servlet;

import com.google.gson.Gson;
import com.yzj.Bean.Cake;
import com.yzj.Bean.Cart;
import com.yzj.Bean.Order;
import com.yzj.android.service.OrderService;
import com.yzj.android.service.impl.OrderServiceImpl;
import com.yzj.android.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

public class OrderServlet extends BaseServlet {
    OrderService orderService = new OrderServiceImpl();
    Gson gson = new Gson();

    protected void myOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(gson.toJson(orderService.myOrder(username)));
    }

    protected void orders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(gson.toJson(orderService.orderList()));
    }


    protected void send(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.StringToInt(req.getParameter("id"),0);
        String username = req.getParameter("username");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");

        orderService.send(id,username);
        resp.getWriter().write("状态：已发货");
    }

    protected void addOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Order order =WebUtils.copyParamToBean(req.getParameterMap(),new Order());
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        orderService.generateOrder(order);
        resp.getWriter().write("已经生成订单");
    }


}
